package com.hgyc.mom.data.service.impl;

import com.hgyc.mom.data.entity.GoviewProjectData;
import com.hgyc.mom.data.service.GoviewProjectDataService;
import com.hgyc.mom.data.vo.GoviewProjectVo;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.data.mapper.GoviewProjectMapper;
import com.hgyc.mom.data.entity.GoviewProject;
import com.hgyc.mom.data.service.GoviewProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import com.hgyc.mom.common.exception.BizException;
import org.springframework.util.CollectionUtils;

/**
 * 大屏项目Service业务层实现
 *
 * @author fwj
 * @since 2025-08-08
 */
@Slf4j
@Service
public class GoviewProjectServiceImpl extends ServiceImpl<GoviewProjectMapper, GoviewProject> implements GoviewProjectService {

    @Autowired
    private GoviewProjectDataService goviewProjectDataService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveProject(GoviewProjectVo goviewProject) {
        goviewProject.setState(-1);
        save(goviewProject);

        if (goviewProject.getContent() != null) {
            GoviewProjectData goviewProjectData = new GoviewProjectData();
            goviewProjectData.setProjectId(goviewProject.getId());
            goviewProjectData.setContent(goviewProject.getContent());
            goviewProjectDataService.save(goviewProjectData);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProject(GoviewProjectVo goviewProject) {
        updateById(goviewProject);
        Long id = goviewProject.getId();

        GoviewProjectData goviewProjectData = goviewProjectDataService.getByProjectId(id);
        if (goviewProjectData != null) {
            goviewProjectData.setContent(goviewProject.getContent());
            goviewProjectDataService.updateById(goviewProjectData);
        } else {
            goviewProjectData.setProjectId(goviewProject.getId());
            goviewProjectData.setContent(goviewProject.getContent());
            goviewProjectDataService.save(goviewProjectData);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeProject(List<Long> ids) {
        removeByIds(ids);

        goviewProjectDataService.removeByProjectIds(ids);
    }
}
